/*
 * @Author: Drc
 * @Date: 2020-06-16 16:56:48
 * @LastEditors: Drc
 * @LastEditTime: 2021-11-03 14:27:20
 */
import React, {useEffect, useRef, useState} from 'react'
import {createStackNavigator, HeaderStyleInterpolators, CardStyleInterpolators} from '@react-navigation/stack'
import {} from 'react-native'
import System from '@path/config/system'
import BackImage from '@path/components/BackImage'
import Theme from '@path/config/theme'
import {ThemeText} from '@path/components/ThemeComponents'
import Login, {LoginParams} from '@path/pages/login/login'
import Guide, {GuideParams} from '@path/pages/guide'

const Stack = createStackNavigator()

//路由配置
export const Screens = {
  Login: {component: Login},
  Guide: {component: Guide},
}

//参数定义
export type Params = {
  Login: LoginParams
  Guide: GuideParams
}

const LoginStack = () => {
  const [isSetup, set_isSetup] = useState(false)
  const isFirstLaunch = useRef<boolean>()

  useEffect(() => {
    _initialRouteName()
  }, [])

  const _initialRouteName = async () => {
    isFirstLaunch.current = await System.isFirst('GuideLaunch', false)
    set_isSetup(true)
  }

  return isSetup ? (
    <Stack.Navigator
      detachInactiveScreens={false} //FIXME:临时解决页面过渡闪烁白边
      initialRouteName={isFirstLaunch.current ? 'Guide' : 'Login'}
      screenOptions={({}) => ({
        headerTitleAlign: 'center',
        headerStyleInterpolator: HeaderStyleInterpolators.forUIKit,
        cardStyleInterpolator: CardStyleInterpolators.forHorizontalIOS,
        headerTransparent: true,
        headerBackTitleVisible: false,
        headerBackTitle: '',
        headerTitle: (e) => {
          return <ThemeText style={{fontSize: 14, fontWeight: '400', ...e.style}}>{e?.children}</ThemeText>
        },
        headerBackImage: () => <BackImage />,
        headerMode: 'float',
      })}>
      {Object.keys(Screens).map((screen) => {
        return <Stack.Screen key={screen} name={screen} component={Screens[screen].component} />
      })}
    </Stack.Navigator>
  ) : null
}

export default LoginStack
